/**
 * Copyright 2008 Matthew DeLambo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.delambo.drool.pooling;

import com.delambo.drool.interfaces.*;
import com.delambo.drool.logging.*;
/**
 * Pool node that has logic for leasing, use timestamps, and resource management.
 * @author delambo
 * @param E type of resource this node manages.
 */
public final class PoolNode_Impl<R extends Resource, T> 
        implements PoolNode<Resource, T> {
    final private R r;
    private boolean inuse = false;
    private long lastUseTimestamp = -1;
    private long leaseTimestamp = -1;
    
    public PoolNode_Impl(R r) {
        this.r = r;
    }
    
    /**
     * Gets the pooling resource from this node.
     * @return nodes resource.
     */
    public T getResource() {
        return (T) this.r.getResource();
    }
    
    /**
     * Kills this nodes resource.
     */
    public void killResource() {
        if(this.r != null)
            this.r.kill();
    }
    
    /**
     * Validates the nodes resource.
     */
    public boolean resourceValidates() {
        if(r != null)
            return this.r.validate();
        else
            return false;
    }
    
    /**
     * Leases node by setting the timestamp and marking it as inuse.
     */
    public void lease() {
        this.inuse = true;
        this.leaseTimestamp = System.currentTimeMillis();
        this.lastUseTimestamp = -1;
    }
    
    /**
     * Expires the lease of this node, resetting it to initial state.
     */
    public void expireLease() {
        reset();
    }
    
    /**
     * Gets the lease time for this node.
     * @return the amount of time node has been leased.
     */
    public long getLeaseTime() {
        return this.leaseTimestamp;
    }
    
    /**
     * Cleans a node by re-initializing it.
     */
    public void cleanUp() {
        reset();
    }
    
    /**
     * Gets the last time this node was inuse.
     * @return the amount of time since last use.
     */
    public long getLastUse() {
        return this.lastUseTimestamp;
    }
    
    /**
     * Returns the result of wether or not this node is inuse.
     * @return true if inuse; otherwise, false.
     */
    public boolean inUse() {
        return this.inuse;
    }
    
    /**
     * Initializes a node by clearing any leasing and use.
     */
    public void init() {
        reset();
    }
    
    /**
     * Resets the nodes state to initial values.
     */
    private void reset() {
        this.r.clean();
        this.inuse = false;
        this.leaseTimestamp = -1;
        this.lastUseTimestamp = System.currentTimeMillis();
    }
}
